<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="一级品类" class="myform-item">
          <el-input
            v-model="form.title"
            placeholder="请输入一级品类"
            clearable
            @keyup.enter.native="onSearch"
          ></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onNew" icon="el-icon-plus"
            >新建品类</el-button
          >
        </el-form-item>
      </div>
    </el-form>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      size="mini"
    >
      <el-table-column
        label="一级品类"
        align="center"
        prop="title"
      ></el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <div v-if="row.status == 1" style="color: #67c23a">已启用</div>
          <div v-else style="color: #f56c6c">已禁用</div>
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        align="center"
        prop="listorder"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" size="mini" @click="onUpdate(row)"
            >修改</el-button
          >
          <el-button type="text" size="mini" @click="onAdd(row)"
            v-if="row.fid == 0">新建二级品类</el-button
          >
          <el-button type="text" size="mini" @click="onEdit(row)"
            v-if="row.fid == 0">编辑</el-button
          >
          <el-button
            type="text"
            v-if="row.status == 1"
            style="color: #f56c6c"
            @click="onDelete(row.id, 0)"
            size="mini"
            >禁用</el-button
          >
          <el-button
            type="text"
            v-else
            style="color: #67c23a"
            @click="onDelete(row.id, 1)"
            size="mini"
            >启用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postApigetcategory"
    />
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="tagform"
        :rules="rules"
        size="mini"
        ref="tagform"
        label-width="190px"
      >
        <template v-if="tagform.fid != 0">
          <el-form-item label="一级品类名称">
            <el-input
              v-model="titlepl"
              placeholder="请输入品类名称"
              clearable
              readonly
              style="width: 70%"
            ></el-input>
          </el-form-item>
          <el-form-item label="二级品类名称" prop="title">
            <el-input
              v-model="tagform.title"
              placeholder="请输入品类名称"
              clearable
              style="width: 70%"
            ></el-input>
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="一级品类名称" prop="title">
            <el-input
              v-model="tagform.title"
              placeholder="请输入品类名称"
              clearable
              style="width: 70%"
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item label="排序(数字越大越靠前)" prop="listorder">
          <el-input
            v-model="tagform.listorder"
            placeholder="请输入排序"
            clearable
            style="width: 70%"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onClickSubmit('tagform')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  postApigetcategory,
  postApieditcategory,
  postApidisablecategory,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false, //审核
      title: "",
      titlepl: "",
      tagform: {
        title: "",
        listorder: "",
        fid: 0,
      },
      rules: {
        title: [{ required: true, message: "请输入品类名称", trigger: "blur" }],
        listorder: [{ required: true, message: "请输入排序", trigger: "blur" }],
      },
      form: {
        title: "",
        fid: 0,
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
    };
  },
  activated() {
    let form = this.form;
    this.postApigetcategory(form);
  },
  methods: {
    async load(tree, treeNode, resolve) {
      console.log("tree", tree);
      let obj = {
        fid: tree.id,
        page: 1,
        size: 10000,
      };
      let res = await postApigetcategory(obj);
      resolve(res.data);
      console.log("treeNode", treeNode);
      console.log("resolve", resolve);
    },
    // 查看
    onEdit(row) {
      console.log(row);
      row = JSON.stringify(row)
      row = this.encrypt(row)
      this.$router.push({
        name: "OperationalDepartmentCategoryEdit",
        query: { row },
      });
    },
    // 新建一级
    onNew() {
      this.title = "新建一级品类";
      this.tagform = {
        title: "",
        listorder: "",
        fid: 0,
      };
      this.dialogVisible = true;
    },
    // 修改
    onUpdate(e) {
        if(e.fid!=0){
            this.title = "修改二级品类";
        }else{
            this.title = "修改一级品类";
        }
      
      this.tagform = {
        id: e.id,
        title: e.title,
        listorder: e.listorder,
        fid: e.fid,
      };
      this.dialogVisible = true;
    },
    // 新建二级
    onAdd(e) {
      this.title = "新建二级品类";
      this.titlepl = e.title;
      this.tagform = {
        title:  "",
        listorder: 0,
        fid: e.id,
      };
      this.dialogVisible = true;
    },
    // 删除
    onDelete(id, status) {
      let obj = {
        id,
        status,
      };
      console.log(obj);
      let { form } = this;
      this.$confirm(`确定${status == 1 ? "启用" : "禁用"}该品类?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postApidisablecategory(obj).then((res) => {
            this.postApigetcategory(form);

          });
        })
        .catch(() => {
       
        });
    },
    // 提交
    onClickSubmit(tagformtext) {
      let that = this;
      let { form, tagform } = this;
      this.$refs[tagformtext].validate(async (valid) => {
        if (valid) {
          tagform.listorder = Number(tagform.listorder);
          postApieditcategory(tagform).then((res) => {
            that.postApigetcategory(form);
            that.$message({
              type: "success",
              message: `新建成功!`,
            });
            this.tagform = {
              title: "",
              font_color: "",
              listorder: "",
              fid: 0,
            };
            this.titlepl = ""
            this.dialogVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 采购列表
    async postApigetcategory(data) {
      this.loading = true;
      this.form = data;
      postApigetcategory(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postApigetcategory(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
